import QtQuick 2.0
import "../Utility.js" as Utils
import "../Constants.js" as Constants

Item {
    property int speed: 2
    property int strength: 3
    property int index: 0
    property int team: 0

    property bool moved: false
    property bool thrown: false
    property bool speced: false

    id: unit
    width: Constants.cellSize
    height: Constants.cellSize

    Image {
        id: fieldImage
        source: "Pawn"+team+"-"+Constants.cellSize+".png"
    }
    MouseArea {
        anchors.fill: parent
        onReleased: {
            unitMenu.visible = true;
        }
    }
    function moveTo(cellX, cellY){
        unit.x = cellX * Constants.cellSize;
        unit.y = cellY * Constants.cellSize;
    }
    function specialTo(cellX, cellY){
        //TODO: implement special actions logic
        console.log("Show specials options")
    }

    function getSelectorIndices(selectorType, units, obstacles, bombs){
        var selectorIndices = new Array();
        var distance = 0;
        switch (selectorType) {
        case "move":
            distance = speed;
            break;
        case "throw":
            distance = strength;
            break;
        case "special":
            distance = 1;
        }
        for (var i=1; i<=distance; i++){
            var leftIndex = Utils.index(Utils.xFromIndex(index)-i, Utils.yFromIndex(index))
            if (leftIndex != -1 &&
                    units[leftIndex] == null &&
                    bombs[leftIndex] == null &&
                    obstacles[leftIndex] == null){
                selectorIndices.push(leftIndex)
            } else {
                break
            }
        }
        for (var i=1; i<=distance; i++){
            var rightIndex = Utils.index(Utils.xFromIndex(index)+i, Utils.yFromIndex(index))
            if (rightIndex != -1 &&
                    units[rightIndex] == null &&
                    bombs[rightIndex] == null &&
                    obstacles[rightIndex] == null){
                selectorIndices.push(rightIndex)
            } else {
                break
            }
        }
        for (var i=1; i<=distance; i++){
            var upIndex = Utils.index(Utils.xFromIndex(index), Utils.yFromIndex(index)-i)
            if (upIndex != -1 &&
                    units[upIndex] == null &&
                    bombs[upIndex] == null &&
                    obstacles[upIndex] == null){
                selectorIndices.push(upIndex)
            } else {
                break
            }
        }
        for (var i=1; i<=distance; i++){
            var downIndex = Utils.index(Utils.xFromIndex(index), Utils.yFromIndex(index)+i)
            if (downIndex != -1 &&
                    units[downIndex] == null &&
                    bombs[downIndex] == null &&
                    obstacles[downIndex] == null){
                selectorIndices.push(downIndex)
            } else {
                break
            }
        }

        return selectorIndices;
    }
}
